package cn.minhx.domain.trade.service.lock;

import cn.minhx.domain.trade.adapter.repostitory.ITradeRepository;
import cn.minhx.domain.trade.moedel.aggregate.GroupBuyOrderAggregate;
import cn.minhx.domain.trade.moedel.entity.*;
import cn.minhx.domain.trade.moedel.valobj.GroupBuyProgressVO;
import cn.minhx.domain.trade.service.ITradeLockOrderService;
import cn.minhx.domain.trade.service.lock.factory.TradeLockRuleFilterFactory;
import cn.minhx.types.design.link.multi.chain.BusinessLinkedList;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 交易订单服务
 *
 * @author min
 */
@Slf4j
@Service
public class TradeLockLockOrderService implements ITradeLockOrderService {

	@Resource
	private ITradeRepository repository;

	@Resource(name = "tradeRuleFilter")
	private BusinessLinkedList<TradeLockRuleCommandEntity, TradeLockRuleFilterFactory.DynamicContext, TradeLockRuleFilterBackEntity> tradeRuleFilter;

	@Override
	public MarketPayOrderEntity queryNoPayMarketPayOrderByOutTradeNo(String userId, String outTradeNo) {
		log.info("拼团交易-查询未支付营销订单 userId:{} outTradeNo:{}", userId, outTradeNo);
		return repository.queryMarketPayOrderEntityByOutTradeNo(userId, outTradeNo);
	}

	@Override
	public GroupBuyProgressVO queryGroupBuyProgress(String teamId) {
		log.info("拼团交易-查询拼单进度 teamId:{}", teamId);
		return repository.queryGroupBuyProgress(teamId);
	}

	@Override
	public MarketPayOrderEntity lockMarketPayOrder(UserEntity userEntity, PayActivityEntity payActivityEntity,
			PayDiscountEntity payDiscountEntity) throws Exception {
		log.info("拼团交易-锁定营销优惠支付订单 userId:{} activityId:{} goodsId:{}", userEntity.getUserId(),
				payActivityEntity.getActivityId(), payDiscountEntity.getGoodsId());
		// 交易规则过滤
		TradeLockRuleFilterBackEntity tradeLockRuleFilterBackEntity = tradeRuleFilter.apply(TradeLockRuleCommandEntity
				.builder().activityId(payActivityEntity.getActivityId()).userId(userEntity.getUserId()).build(),
				new TradeLockRuleFilterFactory.DynamicContext());

		// 已参与拼团量 - 用于构建数据库唯一索引使用，确保用户只能在一个活动上参与固定的次数
		Integer userTakeOrderCount = tradeLockRuleFilterBackEntity.getUserTakeOrderCount();

		// 构建聚合对象
		GroupBuyOrderAggregate groupBuyOrderAggregate = GroupBuyOrderAggregate.builder().userEntity(userEntity)
				.payActivityEntity(payActivityEntity).payDiscountEntity(payDiscountEntity)
				.userTakeOrderCount(userTakeOrderCount).build();
		// 锁定聚合订单 - 这会用户只是下单还没有支付。后续会有2个流程；支付成功、超时未支付（回退）
		return repository.lockMarketPayOrder(groupBuyOrderAggregate);
	}
}
